(function(global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('underscore'), require('./index')) :
    typeof define === 'function' && define.amd ? define(['underscore', './index'], factory) :
    global.apiTest = factory(_, auto);
}(this, function(_, auto) {
  'use strict';

var Random = auto.Random;
var OK = 200;

//带异常模拟
function CC(templet, success, data) {
  var ret = null
  if (success) {
    ret = {
      errorCode: OK,
      message: 'success'
    }
  } else {
    ret = auto({
      'errorCode|1': [
        100, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK,
        101, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK,
        102, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK,
        201, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK,
        202, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK,
        203, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK,
        300, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK,
        301, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK,
        302, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK,
        303, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK,
        304, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK,
        400, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK,
        500, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK
      ],
      'message': function() {
        this.errorCode = OK
        if (this.errorCode === OK) {       
          return 'success';
        } else {
          // return Random.sentence();
          return '这是我们模拟的失败响应。'
        }
      }
    });
  }

  if (ret.errorCode === OK) {
    _.extend(ret, templet ? auto(templet) : data);
  }
  return ret;
}

/**
 * UNIX 时间戳转换成X天X小时X小时X分钟X秒
 * @param {[number]} time [timestamp]
 */
function Timestamp2Range(time) {
  var minTime = parseInt(time / 60)
  var hourTime = parseInt(time / 3600)
  var dateTime = parseInt(time / 86400)
  if (time <= 60) {
    return time + '秒';
  } else if (time >= 60 && time < 3600) {
    return minTime + '分钟' + parseInt(time - minTime * 60) + '秒';
  } else if (time >= 3600 && time < 86400) {
    return hourTime + '小时' + parseInt((time - hourTime * 3600) / 60) + '分钟' + parseInt(time - hourTime * 3600 - parseInt((time - hourTime * 3600) / 60) * 60) + '秒';
  } else if (time >= 86400) {
    return dateTime + '天' + parseInt((time - dateTime * 86400) / 3600) + '小时' + parseInt((time - dateTime * 86400 - parseInt((time - dateTime * 86400) / 3600) * 3600) / 60) + '分钟' + parseInt(time - dateTime * 86400 - parseInt((time - dateTime * 86400) / 3600) * 3600 - parseInt((time - dateTime * 86400 - parseInt((time - dateTime * 86400) / 3600) * 3600) / 60) * 60) + '秒';
  }
}

return {
  makeResponse: CC,

  run: function(options) {
    var info = {}
    var process = options.process;
    if (process) {
      var runTime = process.uptime();
      var bootTimestamp = moment().valueOf() - runTime * 1000;

      info = {
        cmd: process.argv.join(' '),
        pid: process.pid,
        startTime: moment(bootTimestamp).format(),
        runTime: Timestamp2Range(runTime)
      };
    }
    
    return info;
  },
  get: function() {
    var data = CC({
      "data|5-30": [{
        'id|+1': 1,
        guid: '@guid',
        name: '@title',
        description: '@sentence',
        data: '@datetime("yyyy-MM-dd HH:mm:ss,SS")',
        img: '@image',
        'reply_count|0-50': 1,
        'visit_count|0-100': 1,
        "status|1": [0, 1, 2, 3, 4, 5],
        "tab|1": ['share', 'ask']
      }]
    });
    return data;
  },
  xbb: function(count) {
    var data = [];
    var now = +new Date(1997, 9, 3) + 450 * 1000;
    var step = 10 * 1000;
    var value = Math.random() * 1000;
    
    function randomData() {
      now = new Date(+now + step);
      value = value + Math.random() * 21 - 10;
      return {
        date: now.getTime(),
        value: Math.round(value)
      }
    }
    
    count = count || 1;

    for (var i = 0; i < count; i++) {
      data.push(randomData());
    }
    
    return data;
  },
  gis: function() {
    var data = CC({
      "data|50": [{
        'id|+1': 1,
        'lon|119-121.4': 120,
        'lat|29-31.4': 30,
        'unixTime|+1': 1567497450,
        'msTime': function() {
          return this.unixTime * 1000;
        },
        'state|1': [0, 1],
        'status': function() {
          return ['off', 'on'][this.state];
        },
        'statusC': function() {
          return ['离线', "在线"][this.state];
        },
        'alert|1': [0, 1, 2, 3],
        'severity': function() {
          return ['ok', 'warning', 'error', 'critical'][this.alert];
        },
        'severityC': function() {
          return ['恢复', '提醒', '错误', '紧急'][this.alert]
        },
        'alertCount|1-100.10': 1
      }]
    });
    return data;
  }
}

}))